package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.mapper.EduCommentMapper;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-24
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {
    /**
     * 分页查询评论
     *
     * @param commentPage
     * @param courseId
     * @return
     */
    @Override
    public Map<String, Object> getPageComment(Page<EduComment> commentPage, String courseId) {
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        // 判断Id是否为空
        if (StringUtils.hasText(courseId)) {
            queryWrapper.eq("course_id", courseId);
        }
        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(commentPage, queryWrapper);
        List<EduComment> commentList = commentPage.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", commentPage.getCurrent());
        map.put("pages", commentPage.getPages());
        map.put("size", commentPage.getSize());
        map.put("total", commentPage.getTotal());
        map.put("hasNext", commentPage.hasNext());
        map.put("hasPrevious", commentPage.hasPrevious());
        return map;
    }
}
